/**
 * Created by zoulu on 15/10/14.
 */
var base_city = "";
var base_name = "";
var Manage = {

	init: function() {
		Manage.get_data();
		$(".pagination li a").click(function() {
			var page = $(this).attr("page");
			var size = 10;
			Manage.get_data(page, size);
			Common.init_pagination(page);
		});

		$("#data_table tbody").find("a[name='update_venue']").click(Manage.venue_show);
		$("#data_table tbody").find("a[name='select_lessons']").click(Manage.select_lesson);
		$("#city_id").change(Manage.change_city);
		$("a[name='add_venue']").click(Manage.venue_show);

		$("#data_table tbody").find("a[name='remove_class']").click(function() {
			var venue_id = $(this).attr("venue_id");
			var class_id = $(this).attr("class_id");
			ajax("/venue/classes/remove", {
				"venue_id": venue_id,
				"class_ids": class_id
			}, success);
		});
		$("#do_update_venue").click(function() {
			var brand_name = $("#brand_id option:selected").text();
			var venue_status = $("#venue_status option:selected").val();
			var brand_id = $("#brand_id option:selected").val();
			var venue_id = $("#update_venue").find("input[name='venue_id']").val();
			var venue_name = $("#update_venue").find("input[name='venue_name']").val();
			var city_id = $("#city_id").find("option:selected").val();
			var city_id1 = $("#city_id1").find("option:selected").val();
			var venue_city = "";
			if (city_id != null && city_id != "" && city_id1 != null && city_id1.length > 0) {
				venue_city = city_id1;
			} else if (city_id != null && city_id != "") {
				venue_city = city_id
			}
			var venue_address = $("#update_venue").find("input[name='venue_address']").val();
			var venue_tel = $("#update_venue").find("input[name='venue_tel']").val();
			var venue_transport = $("#update_venue").find("input[name='venue_transport']").val();
			var venue_wiki = $("#update_venue").find("input[name='venue_wiki']").val();
			var venue_open_at = $("#update_venue").find("input[name='venue_open_at']").val();
			var venue_close_at = $("#update_venue").find("input[name='venue_close_at']").val();
			var date_open = new Date(Date.parse(venue_open_at)).getTime() / 1000;
			var date_close = new Date(Date.parse(venue_close_at)).getTime() / 1000;

			if (venue_name == null || venue_name == "") {
				alert("场馆名称不能为空");
				return false;
			}
			if (brand_id == null || brand_id == "") {
				alert("品牌不能为空");
				return false;
			}
			if (venue_city == null || venue_city == "") {
				alert("所在城市不能为空");
				return false;
			}
			if (venue_address == null || venue_address == "") {
				alert("详细地址不能为空");
				return false;
			}
			if (date_open > date_close) {
				alert("营业时间大于关门时间");
				return false;
			}
			if (venue_id == "" || venue_id == 0) {
				//新增
				ajax("/schools/create", {
					"brand_name": brand_name,
					"name": venue_name,
					"city": venue_city,
					"address": venue_address,
					"tel": venue_tel,
					"transport": venue_transport,
					"wiki": venue_wiki,
					"open_at": date_open,
					"close_at": date_close
				}, success);
			} else {
				//编辑
				ajax("/schools/update", {
					"school_id": venue_id,
					"brand_id": brand_id,
					"name": venue_name,
					"status": venue_status,
					"tel": venue_tel,
					"transport": venue_transport,
					"wiki": venue_wiki,
					"open_at": date_open,
					"close_at": date_close
				}, success);
			}
		});
		$("#do_add_class").click(function() {
			var venue_id = $("#add_class").find("input[name='venue_id']").val();
			var class_id = $("#add_class").find("select[name='class_id']").val();
			if (class_id == null || class_id == "") {
				alert("必须选择一个科目");
			}
			ajax("/categories/classes/add", {
				"venue_id": venue_id,
				"class_ids": class_id
			}, success);
		});
	},
	get_data: function(page, size) {
		if (page == undefined) {
			page = 0;
		}
		if (size == undefined) {
			size = 10;
		}
		ajax("/schools/list", {
			"page": page,
			"size": size
		}, callback_init_table);
	},
	venue_show: function() {
		var brand_id = $(this).attr("brand_id");
		var venue_id = $(this).attr("venue_id");
		var venue_name = $(this).attr("venue_name");
		var venue_status = $(this).attr("venue_status");
		var venue_city_name = $(this).attr("venue_city_name");
		var venue_city = $(this).attr("venue_city");
		base_city = venue_city;
		base_name = venue_city_name;
		var venue_address = $(this).attr("venue_address");
		var venue_tel = $(this).attr("venue_tel");
		var venue_transport = $(this).attr("venue_transport");
		var venue_wiki = $(this).attr("venue_wiki");
		var venue_open_at = $(this).attr("venue_open_at");
		var venue_close_at = $(this).attr("venue_close_at");
		Manage.brand_get(brand_id);
		asyAjax.post("/cities", {}, callback_init_city)

		$("#update_venue").find("input[name='venue_id']").val(venue_id);
		$("#update_venue").find("input[name='venue_name']").val(venue_name);
		$("#update_venue").find("input[name='venue_city']").val(venue_city);
		$("#update_venue").find("input[name='venue_address']").val(venue_address);
		$("#update_venue").find("input[name='venue_tel']").val(venue_tel);
		$("#update_venue").find("input[name='venue_transport']").val(venue_transport);
		$("#update_venue").find("input[name='venue_wiki']").val(venue_wiki);

		$("#venue_status option").each(function() {
			if ($(this).value == venue_status) {
				$(this).selected = true;
			}
		});

		if (venue_id != "" && venue_id != 0) {
			$("#update_venue").find("input[name='venue_open_at']").val(formatDate(venue_open_at));
			$("#update_venue").find("input[name='venue_close_at']").val(formatDate(venue_close_at));
		}

		$("#update_venue").modal('show');
	},
	brand_get: function(id) {
		var host = "http://103.37.147.254:9998/adminhost";
		var session_id = $.cookie("session_id");

		$.ajax({
			url: host + "/brands/list",
			type: 'post',
			async: true,
			data: {
				"sid": session_id
			},
			dataType: 'json',
			success: function(result) {

				if (result.code == 0) {
					var brands = result.data.brands;
					var trs = "";
					$("#brand_id").find("option").remove();

					for (var i = 0; i < brands.length; i++) {

						if (brands[i].id == id) {
							var varItem = "<option selected=\"selected\" value ='" + brands[i].id + "'>" + brands[i].name + "</option>";
						} else {
							var varItem = "<option value ='" + brands[i].id + "'>" + brands[i].name + "</option>";
						}
						$("#brand_id").append(varItem);
					}
					return;
				} else {
					return "";
				}
			}
		});
	},

	init_brands: function(result) {
		if (result != null && result.data != null && result.data.brands.length > 0) {
			var brands = result.data.brands;
			var trs = "";
			for (var i = 0; i < brands.length; i++) {
				var varItem = "<option value ='" + brands[i].id + "'>" + brands[i].name + "</option>";
				$("#brand_id").append(varItem);
			}
		}
	},

	init_table: function(result) {
		if (result != null && result.data != null && result.data.schools.length > 0) {
			var tbody = $("#data_table tbody");
			tbody.empty();
			var schools = result.data.schools;
			var brands = result.data.brands;
			var map = new Map();
			for (var i = 0; i < brands.length; i++) {
				map.put(brands[i].id, brands[i].name);
			}
			var trs = "";
			for (var i = 0; i < schools.length; i++) {
				var venue = schools[i];

				trs += ("<tr>");
				var venuebrand_id = ("<td >" + map.get(venue.brand_id) + "</td>");
				var venueName = ("<td >" + venue.name + "</td>");
				var venueCity = ("<td >" + venue.city_name + "</td>");
				var venueAdress = ("<td >" + venue.address + "</td>");
				var venueTel = ("<td >" + venue.tel + "</td>");
				var venueTransport = ("<td >" + venue.transport + "</td>");
				var venueWiki = ("<td >" + venue.wiki + "</td>");
				var venueOpenAt = ("<td >" + formatDate(venue.open_at) + "</td>");
				var venueCloseAt = ("<td >" + formatDate(venue.close_at) + "</td>");


				var venueOperate = ("<td>" +
					"<a venue_id='" + venue.id + "' venue_status='" + venue.status + "' venue_city_name='" + venue.city_name + "' brand_id='" + venue.brand_id + "' venue_name='" + venue.name + "' venue_city='" + venue.city + "' venue_address='" + venue.address +
					"' venue_tel='" + venue.tel + "' venue_transport='" + venue.transport + "' venue_wiki='" + venue.wiki + "' venue_open_at='" + venue.open_at +
					"' venue_close_at='" + venue.close_at +
					"' name='update_venue' href='javascript:void(0)'>更新场馆</a>" +
					"&nbsp;&nbsp;<a name='select_lessons' href='./venue_lesson.html?school_id=" + venue.id + "'>查看场馆课程列表</a>" +
					"&nbsp;&nbsp;<a name='select_photos' href='./venue_photo.html?school_id=" + venue.id + "'>查看场馆图片</a>" +
					"&nbsp;&nbsp;<a name='select_photos' href='./rooms_list.html?school_id=" + venue.id + "'>查看场馆教室</a>" +
					"</td>");
				trs += (venuebrand_id + venueName + venueCity + venueAdress + venueTel + venueTransport + venueWiki + venueOpenAt + venueCloseAt + venueOperate);
				trs += ("</tr>");
			}
			tbody.append(trs);
		}
	},
	change_city: function() {
		var citys = $("#city_id option:selected").attr("citys");

		if (citys != undefined && citys.length > 0) {
			citys = eval("(" + citys + ")");
			if (citys.length > 0) {
				var city_id = $("#city_id1");
				city_id.empty();
				var options = "";
				for (var i = 0; i < citys.length; i++) {
					var city = citys[i];
					if (city.code == base_city) {
						options += ("<option selected=\"selected\" value = " + city.code + ">" + city.name + "</option>");
					} else {
						options += ("<option value = " + city.code + ">" + city.name + "</option>");
					}

				}
				city_id.append(options);
			}
		}

	},
	init_city: function(result) {

		result = eval("(" + result + ")");
		if (result != null && result.data != null && result.data.provinces.length > 0) {
			var city_id = $("#city_id");
			$("#city_id").find("option").remove();
			var city_id1 = $("#city_id1");
			$("#city_id1").find("option").remove();
			var citis = result.data.provinces;
			var options = "";
			options += ("<option  value = ''></option>");
			for (var i = 0; i < citis.length; i++) {
				var city = citis[i];
				var city1 = JSON.stringify(city.places);

				if (city.places.length > 0 ) {
					if(base_city=="" || base_name==""){
						options += ("<option  citys='" + city1 + "' value = '" + city.code + "'>" + city.name + "</option>");
					}
					
					else if(city1.indexOf(base_name) > 0){
						options += ("<option selected=\"selected\" citys='" + city1 + "' value = '" + city.code + "'>" + city.name + "</option>");
						var citis1 = eval("(" + city1 + ")");
						var optionsj = "";
						
						for (var j = 0; j < citis1.length; j++) {
							var cityj = citis1[j];
							if (cityj.name == base_name) {
								optionsj += ("<option selected=\"selected\" value = " + cityj.code + ">" + cityj.name + "</option>");
							} else {
								optionsj += ("<option value = " + cityj.code + ">" + cityj.name + "</option>");
							}
						}
						city_id1.append(optionsj);
					}
					else{
							options += ("<option  citys='" + city1 + "' value = '" + city.code + "'>" + city.name + "</option>");
					}
				} else {
					options += ("<option  citys='" + city1 + "' value = '" + city.code + "'>" + city.name + "</option>");
				}
			}
			city_id.append(options);
		}
	}
};

function callback_init_brands(result) {
	Manage.init_brands(result);
}

function callback_init_table(result) {
	Manage.init_table(result);
}

function callback_init_city(result) {
	Manage.init_city(result);
}

function success(result) {
	if (result != null && result.code == 0) {
		window.location.reload();
	} else {
		alert("操作失败，请刷新页面重试！");
	}
}

$(function() {
	$('.form_datetime').datetimepicker({
		//language:  'fr',
		weekStart: 1,
		todayBtn: 1,
		autoclose: 1,
		todayHighlight: 1,
		startView: 2,
		forceParse: 0,
		showMeridian: 1
	});
});

$(function() {
	Manage.init();
})